package cn.org.donald.framework.handler;

import cn.org.donald.framework.service.SysTableService;
import cn.org.donald.service.game.TableService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author ： Donald
 * @date ： 2020/11/25 22:13
 * @description： 物理清除数据库已删除记录调度任务
 */
@Component
public class CleanDataBaseExecutor {

    private static Logger logger = LoggerFactory.getLogger(CleanDataBaseExecutor.class);

    @Reference(interfaceClass = SysTableService.class, version = "1.0.0", group = "donald")
    private SysTableService sysTableService;

    @Reference(interfaceClass = TableService.class,version = "1.0.0",group = "donald")
    private TableService tableService;

    public void init(){
        logger.info("executor init");
    }

    public void destroy(){
        logger.info("executor destroy");
    }

    @XxlJob("cleanDataBaseExecutor")
    public ReturnT<String> execute(String msg) throws Exception{
        return getLogger(msg, sysTableService.cleanTable());
    }

    /**
     * 物理清除donald_game的已删除记录
     * @param msg
     * @return
     * @throws Exception
     */
    @XxlJob("cleanDataBaseGameExecutor")
    public ReturnT<String> cleanDataBaseGameExecutor(String msg) throws Exception{
        return getLogger(msg, tableService.cleanTable());
    }

    private ReturnT<String> getLogger(String msg, int rows){
        logger.info("executor is running, from {}{}",this.getClass().getName(),msg==null?"":",task msg: "+msg);
        logger.info("Executor {} run successfully, has deleted {} rows",this.getClass().getName(),rows);
        XxlJobLogger.log("has deleted" + rows + "rows");
        return ReturnT.SUCCESS;
    }
}
